package com.upay.android.controller.adapter;

import static com.upay.android.controller.helper.Format.format;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.RadioButton;
import android.widget.TextView;

import com.upay.android.R;
import com.upay.android.controller.helper.Format;
import com.upay.android.domain.payment.FixedPaymentDetail;
import com.upay.android.domain.payment.PaymentList;
import com.upay.android.domain.user.UserWrapper;

public class FixedPaymentArrayAdapter extends ArrayAdapter<FixedPaymentDetail> {
	
	/* ---- Constants ---- */
	
	/* ---- Instance Variables ---- */
	private Activity mContext = null;
	private PaymentList<FixedPaymentDetail> mPaymentList = null;


	/* ---- Constructors ---- */
	
	public FixedPaymentArrayAdapter(Activity context, PaymentList<FixedPaymentDetail> paymentList) {
		super(context, R.layout.fragment_row_fixed_payment, paymentList.getPayments());
		this.mContext = context;
		this.mPaymentList = paymentList;
	}
	
	/* ---- ViewHolder ---- */
	
	static class ViewHolder {
		public TextView fullName = null;
		public TextView groupBalanceValue = null;
		public TextView transactionLineValue = null;
		public RadioButton selectionButton = null;
	}
	
	/* ---- Business Methods ---- */
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View rowView = convertView;
		if (rowView == null) {
			LayoutInflater inflator = mContext.getLayoutInflater();
			rowView = inflator.inflate(R.layout.fragment_row_fixed_payment, null);
			
			final ViewHolder viewHolder = new ViewHolder();
			viewHolder.fullName = (TextView)rowView.findViewById(R.id.value_fixed_payment_fullname);
			viewHolder.groupBalanceValue = (TextView)rowView.findViewById(R.id.value_fixed_payment_group_balance);
			viewHolder.transactionLineValue = (TextView)rowView.findViewById(R.id.value_fixed_payment_transaction_line);
			viewHolder.selectionButton = (RadioButton)rowView.findViewById(R.id.button_fixed_payment_selected);
	
			rowView.setTag(viewHolder);
			viewHolder.selectionButton.setTag(mPaymentList.get(position));
		} else {
			rowView = convertView;
			((ViewHolder)rowView.getTag()).selectionButton.setTag(mPaymentList.get(position));
		}
		
		FixedPaymentDetail fixedPayment = mPaymentList.get(position);
		ViewHolder viewHolder = (ViewHolder)rowView.getTag();
		viewHolder.selectionButton.setChecked(fixedPayment.isSelected());
		viewHolder.fullName.setText(UserWrapper.getFullName(fixedPayment.getGroupAccount().getUser()));
		viewHolder.transactionLineValue.setText(format(fixedPayment.getTransactionLine().getValue()));
		Format.drawBalance(viewHolder.groupBalanceValue, fixedPayment.getGroupAccount().getBalance());
		viewHolder.groupBalanceValue.setText(format(fixedPayment.getGroupAccount().getBalance()));

		viewHolder.selectionButton.setChecked(mPaymentList.getPayments().get(position).isSelected());
		viewHolder.selectionButton.setOnClickListener(new View.OnClickListener() {
		
			@Override
			public void onClick(View view) {
				mPaymentList.getSelectedPayment().setSelected(Boolean.FALSE);
				RadioButton button = (RadioButton)view; 
				FixedPaymentDetail fixedPayment = ((FixedPaymentDetail)button.getTag());				
				fixedPayment.setSelected(Boolean.TRUE);
				button.setSelected(Boolean.TRUE);
				mPaymentList.setSelectedPayment(fixedPayment);
				notifyDataSetChanged();
			}
		});
		return rowView;
	}
	
	/* ---- Getters and Setters ---- */

	/**
	 * @return the context
	 */
	public Activity getContext() {
		return mContext;
	}

	/**
	 * @param context the context to set
	 */
	public void setContext(Activity context) {
		this.mContext = context;
	}

}
